package com.yd.tzkk.act;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.xuexiang.xutil.tip.ToastUtils;
import com.yd.tzkk.R;
import com.yd.tzkk.update.XUpdateInit;
import com.yd.tzkk.web.JavaScriptInterface;
import com.yd.tzkk.web.MyWebViewClient;

public class WebAct extends Activity {
    protected WebView mWebView;
    private long mBackPressed;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.act_web);
        super.onCreate(savedInstanceState);
        mWebView = findViewById(R.id.webview);

        initWebView();

        Bundle bundle = getIntent().getExtras();
        String url = bundle.getString("url","about:blank");
        mWebView.loadUrl(url);

        XUpdateInit.checkUpdate(this,true);
    }

    private void initWebView(){
        WebSettings webSettings = mWebView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            webSettings.setSafeBrowsingEnabled(false);
        }
        webSettings.setJavaScriptEnabled(true); // 启用JavaScript

        //localStorage  允许存储
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheMaxSize(1024*1024*8);//存储的最大容量
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setSupportZoom(false);

        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.setOverScrollMode(View.OVER_SCROLL_NEVER);//去掉滑动边界时产生回弹和阻尼效果
        mWebView.setVerticalScrollBarEnabled(false);//禁用滚动条
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.addJavascriptInterface(new JavaScriptInterface(mWebView), "AppInterface");
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            String url = mWebView.getUrl();
            if(!url.endsWith("#/index")&&!url.endsWith("#/invest")&&!url.endsWith("#/team")&&!url.endsWith("#/user")){
                mWebView.goBack();
                return;
            }
        }
        backExit();
    }
    protected void backExit(){
        int TIME_INTERVAL = 2000;
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()){
            finish();
            return;
        }else{
            ToastUtils.toast("再按一次退出");
        }
        mBackPressed = System.currentTimeMillis();
    }
}
